﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using NUnit.Framework;

namespace ReferenceSwap.Tests
{
    [TestFixture]
    public class SolutionDataFixture
    {
        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void VerifyNullStringCheck_ctor()
        {
            new SolutionData(null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void VerifyEmptyStringCheck_ctor()
        {
            new SolutionData("");
        }

        [Test]
        [ExpectedException(typeof(FileNotFoundException))]
        public void VerifyInvalidFileCheck_ctor()
        {
            new SolutionData("ReferenceSwap.sln");
        }

        [Test]
        public void TestSolutionParse()
        {
            int expectedCount = 5;
            var target = new SolutionData(TestData.SolutionFile);

            Assert.AreEqual(expectedCount, target.ProjectReferences.Count);
        }

        [Test]
        public void TestSolutionProjectReferenceParse()
        {
            var solution = new SolutionData(TestData.SolutionFile);
            string expectedName = "ReferenceSwap.Nant";
            string expectedPath = @"ReferenceSwap.Nant\ReferenceSwap.Nant.csproj";
            string expectedGuid = "{9c376f74-d685-4b1f-967f-03178eb987b4}";

            Assert.Greater(solution.ProjectReferences.Count, 0);
            var target = solution.ProjectReferences[0];

            Assert.AreEqual(expectedName, target.Name);
            Assert.AreEqual(expectedPath, target.Path);
            Assert.AreEqual(expectedGuid, target.Guid.ToLower());
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void VerifyNullCheck_RemoveProjectReference()
        {
            var target = new SolutionData(TestData.SolutionFile);
            target.RemoveProjectReference(null);
        }

        [Test]
        public void TestRemoveProjectReference()
        {
            int expectedCountBeforeRemove = 5;
            int expectedCount = 4;

            var target = new SolutionData(TestData.SolutionFile);

            Assert.AreEqual(expectedCountBeforeRemove, target.ProjectReferences.Count);
            var reference = target.ProjectReferences[0];

            target.RemoveProjectReference(reference);
            Assert.AreEqual(expectedCount, target.ProjectReferences.Count);
        }

        /// <summary>
        /// Related to Issue 1
        /// https://refswap.codeplex.com/workitem/1
        /// </summary>
        [Test]
        public void VerifyFirstLineIsNotEmpty()
        {
            var target = new SolutionData(TestData.SolutionFile);
            var reference = target.ProjectReferences[0];
            target.RemoveProjectReference(reference);

            target.Save();
            using (var reader = new StreamReader(TestData.SolutionFile))
            {
                string line = reader.ReadLine();
                Assert.IsNotEmpty(line);
            }
        }
    }
}
